06. C++ Simulator
Now that you have all the code on your computer and the simulator running, let's walk through some of the elements of the code and the simulator itself.
The Code
For the project, the majority of your code will be written in src/QuadControl.cpp
. This file contains all of the code for the controller that you will be developing.
All the configuration files for your controller and the vehicle are in the config
directory. For example, for all your control gains and other desired tuning parameters, there is a config file called QuadControlParams.txt
set up for you. An import note is that while the simulator is running, you can edit this file in real time and see the affects your changes have on the quad!
The syntax of the config files is as follows:
[Quad]
begins a parameter namespace. Any variable written afterwards becomesQuad.<variablename>
in the source code.- If not in a namespace, you can also write
Quad.<variablename>
directly. [Quad1 : Quad]
means that theQuad1
namespace is created with a copy of all the variables ofQuad
. You can then overwrite those variables by specifying new values (e.g.Quad1.Mass
to override the copiedQuad.Mass
). This is convenient for having default values.
You will also be using the simulator to fly some different trajectories to test out the performance of your C++ implementation of your controller. These trajectories, along with supporting code, are found in the traj
directory of the repo.
The Simulator
In the simulator window itself, you can right click the window to select between a set of different scenarios that are designed to test the different parts of your controller.
The simulation (including visualization) is implemented in a single thread. This is so that you can safely breakpoint code at any point and debug, without affecting any part of the simulation.
Due to deterministic timing and careful control over how the pseudo-random number generators are initialized and used, the simulation should be exactly repeatable. This means that any simulation with the same configuration should be exactly identical when run repeatedly or on different machines.
Vehicles are created and graphs are reset whenever a scenario is loaded. When a scenario is reset (due to an end condition such as time or user pressing the ‘R’ key), the config files are all re-read and state of the simulation/vehicles/graphs is reset -- however the number/name of vehicles and displayed graphs are left untouched.
When the simulation is running, you can use the arrow keys on your keyboard to impact forces on your drone to see how your controller reacts to outside forces being applied.
Keyboard / Mouse Controls
There are a handful of keyboard / mouse commands to help with the simulator itself, including applying external forces on your drone to see how your controllers reacts!
- Left drag - rotate
- X + left drag - pan
- Z + left drag - zoom
- arrow keys - apply external force
- C - clear all graphs
- R - reset simulation
- Space - pause simulation
Testing it Out
When you run the simulator, you'll notice your quad is falling straight down. This is due to the fact that the thrusts are simply being set to:
QuadControlParams.Mass * 9.81 / 4
Therefore, if the mass doesn't match the actual mass of the quad, it'll fall down. Take a moment to tune the Mass
parameter in QuadControlParams.txt
to make the vehicle more or less stay in the same spot.
Note: if you want to come back to this later, this scenario is "1_Intro".
Almost There
Now that you have the simulator and skeleton code up and running, you're almost ready to start writing your controller! One last thing to see is some of the tools we have provided you for evaluating the performance of your controller and some general tips and tricks for the project.